package com.other;


import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * Created by baiguantao on 2017/10/9.
 */
public class EmailUtils {
    private static  String emailserve="1107530849@qq.com";
    private static String emailservepwd="itnxtgsetebxggce";//pop3smtp
    public  static    void   SendMails(String text,String Subject,String mail){

        try {
            System.out.println("邮件服务发送开始！！！！！");
            Properties props = new Properties();

            // 开启debug调试
            props.setProperty("mail.debug", "true");
            // 发送服务器需要身份验证
            props.setProperty("mail.smtp.auth", "true");
            // 设置邮件服务器主机名
            String smtpHostName = "smtp." + mail.split("@")[1];
            props.setProperty("mail.host", smtpHostName);
            // 发送邮件协议名称
            props.setProperty("mail.transport.protocol", "smtp");
            props.setProperty("mail.debug","false");
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", sf);

            Session session = Session.getInstance(props);
//            session.setDebug(false);//关闭mail日志
            Message msg = new MimeMessage(session);
            msg.setSubject(Subject);
            msg.setText("ip来源:"+IpUtils.getLocalIPList()+"\n异常内容:\n"+text+"\n推送时间:"+getNowTimeString());
            msg.setFrom(new InternetAddress(emailserve));

            Transport transport = session.getTransport();
            transport.connect("smtp.qq.com", emailserve, emailservepwd);

            transport.sendMessage(msg, new Address[] { new InternetAddress(mail) });
            transport.close();
            System.out.println("邮件服务发送成功！！！！！");
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
    public static String getNowTimeString(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return sdf.format(new Date());
    }

    public static void main(String[] args) {
//        EmailUtils.SendMails("错误信息: ","消息队列异常","1107530849@qq.com");
    nopassword();
  }
    public static  void nopassword()  {
        try {


        Properties props = new Properties();
        props.put("mail.smtp.host", "127.0.0.1");
        props.put("mail.smtp.port", "25");
        props.put("mail.debug", "true");
        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("admin@test.com"));
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("1107530849@qq.com"));
        message.setSubject("Notification");
        message.setText("Successful!", "UTF-8"); // as "text/plain"
        message.setSentDate(new Date());
        Transport.send(message);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
